﻿using System;
using System.Diagnostics;
using System.Windows.Forms;

namespace ProxyMA
{
    public partial class ProxyForm : Form
    {
        #region Delegates

        public delegate void LogDelegate(string log);

        #endregion

        private readonly string fileName;
        private readonly LogDelegate logDelegate;
        private Process process;

        public ProxyForm(string fileName)
        {
            this.fileName = fileName;

            logDelegate = new LogDelegate(OutputlogToTextBox);

            InitializeComponent();
        }

        public void Start()
        {
            var processStartInfo = new ProcessStartInfo(fileName)
                                       {
                                           CreateNoWindow = true,
                                           WindowStyle = ProcessWindowStyle.Hidden,
                                           UseShellExecute = false,
                                           RedirectStandardOutput = true,
                                           RedirectStandardError = true,
                                           RedirectStandardInput = true
                                       };

            process = Process.Start(processStartInfo);

            process.OutputDataReceived += ProcessOutputDataReceived;
            process.ErrorDataReceived += ProcessErrorDataReceived;

            process.BeginOutputReadLine();
            process.BeginErrorReadLine();

            Show();
        }


        void ProcessErrorDataReceived(object sender, DataReceivedEventArgs e)
        {
            try
            {
                OutputlogToTextBox(e.Data);
            }
            catch (Exception ex)
            {
                if (!process.HasExited)
                    process.Kill();
                Close();
            }
        }

        void ProcessOutputDataReceived(object sender, DataReceivedEventArgs e)
        {
            try
            {
                OutputlogToTextBox(e.Data);
            }
            catch (Exception ex)
            {
                if (!process.HasExited)
                    process.Kill();
                Close();
            }
        }

        private void OutputlogToTextBox(string log)
        {
            if (InvokeRequired)
            {
                Invoke(logDelegate, log);
            }
            else
            {
                if (outputTextBox.TextLength > 80960)
                {
                    outputTextBox.Clear();
                }
                outputTextBox.Paste(log);
                outputTextBox.Paste(Environment.NewLine);
            }
        }

        private void ProxyFormFormClosing(object sender, FormClosingEventArgs e)
        {
            if (!process.HasExited)
                process.Kill();
        }

        private void AliveTimer_Tick(object sender, EventArgs e)
        {
            if (process.HasExited)
                Start();
        }
    }
}